#include "theaterconst.h"

/* Concession Clerk */
void main() {
	int data;
	
	init();
	
	Acquire(idMutex);
	data = ReadMonitor(ccID, 0);
	prints("ConcessionClerk "); print(data); prints(" has entered the theater.\n");
	SetMonitor(ccID, 0, data + 1);
	Release(idMutex);

	while (1) {
		Acquire(ccLineLock);
		if (ReadMonitor(ccBreak, data) != 0) {
			/* go on a break. wake all customers */
			SetMonitor(ccState, data, C_BREAK);
			/* Acquire(outputLock); */
			prints("ConcessionClerk "); print(data); 
			prints(" is going on break.\n");
			/* Release(outputLock); */
			SetMonitor(ccBreakTime, data, ReadMonitor(ccBreakTime, data) + 1);
			SetMonitor(ccLineCount, data, 0);
			Broadcast(ccLineCV[data], ccLineLock);
			Wait(ccBreakCV, ccLineLock);
			SetMonitor(ccState, data, C_BUSY);
			/* Acquire(outputLock); */
			prints("ConcessionClerk "); print(data); 
			prints(" is coming off break.\n");
			/* Release(outputLock); */
			SetMonitor(ccBreak, data, 0);
		}
		if (ReadMonitor(ccLineCount, data) > 0) {
			/* there's someone in line. signal a customer */
			/* Acquire(outputLock); */
			prints("ConcessionClerk "); print(data); 
			prints(" has a line length "); print(ReadMonitor(ccLineCount, data));
			prints(" and is signaling a customer.\n");
			/* Release(outputLock); */
			SetMonitor(ccLineCount, data, ReadMonitor(ccLineCount, data) - 1);
			SetMonitor(ccState, data, C_BUSY);
			Signal(ccLineCV[data], ccLineLock);
		} else {
			/* no one in line. set ready */
			SetMonitor(ccState, data, C_READY);
			/* Acquire(outputLock); */
			prints("ConcessionClerk "); print(data); 
			prints(" has no one in line. I am available for a customer.\n");
			/* Release(outputLock); */
		}
		Acquire(ccLock[data]);
		Release(ccLineLock);
		Wait(ccCV[data], ccLock[data]);
		Acquire(ccLineLock);
		if (ReadMonitor(ccState, data) == C_BREAK) {
			/* waken up by manager. go on a break */
			Release(ccLock[data]);
			/* Acquire(outputLock); */
			prints("ConcessionClerk "); print(data); 
			prints(" is going on break.\n");
			/* Release(outputLock); */
			SetMonitor(ccBreakTime, data, ReadMonitor(ccBreakTime, data) + 1);
			Wait(ccBreakCV, ccLineLock);
			/* Acquire(outputLock); */
			prints("ConcessionClerk "); print(data); 
			prints(" is coming off break.\n");
			/* Release(outputLock); */			
			SetMonitor(ccState, data, C_BUSY);
			SetMonitor(ccBreak, data, 0);
			Release(ccLineLock);
			continue;
		}
		Release(ccLineLock);
		/* interact with the head customer */
		SetMonitor(ccPrice, data, ReadMonitor(ccPopcorn, data) * POPCORN_PRICE + ReadMonitor(ccSoda, data) * SODA_PRICE);
		/* Acquire(outputLock); */
		prints("ConcessionClerk "); print(data);
		prints(" has an order for "); print(ReadMonitor(ccPopcorn, data));
		prints(" popcorn and "); print(ReadMonitor(ccSoda, data)); prints(" soda. The cost is $");
		print(ReadMonitor(ccPrice, data)); prints(".00.\n");
		/* Release(outputLock); */
		Signal(ccCV[data],ccLock[data]);
		Wait(ccCV[data],ccLock[data]);
		/* get the money */
		Acquire(incomeMutex);
		SetMonitor(ccIncome, data, ReadMonitor(ccIncome, data) + ReadMonitor(ccPrice, data));
		SetMonitor(totalIncome, 0, ReadMonitor(totalIncome, 0) + ReadMonitor(ccPrice, data));
		Release(incomeMutex);
		Signal(ccCV[data], ccLock[data]);
		Wait(ccCV[data], ccLock[data]);
		Release(ccLock[data]);
	}
	Exit(0);
}